package com.objectdraw.client.objectdraw;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

/**
 * Instantiated to store a rectangle object. 
 * 
 * @author jgulik, cfruehan
 *
 */
public class RectangleObject implements IDrawingObject {

	Color myColor;
	Point p1,p2;
	
	/**
	 * constructor used to instantiate rectangle object with 2 points and a color
	 * @param p1 corner point of rectangle
	 * @param p2 opposite corner point of rectangle
	 * @param c color of rectangle
	 */
	public RectangleObject(Point p1,Point p2, Color c){

		myColor=c;
		this.p1=p1;
		this.p2=p2;
		//System.out.println("made new fdo");
	}
	
	/**
	 * overloaded constructor using strings instead of points and color
	 * @param x1 X1 coord
	 * @param y1 Y1 Coord
	 * @param x2 X2 Coord
	 * @param y2 Y2 Coord
	 * @param color Color of object
	 */
	public RectangleObject(String x1, String y1, String x2, String y2, String color){
		
		if(color.equalsIgnoreCase("red")){
			myColor = Color.RED;
		}else if(color.equalsIgnoreCase("blue")){
			myColor = Color.BLUE;
		}else if(color.equalsIgnoreCase("green")){
			myColor = Color.GREEN;
		}else{
			myColor = Color.BLACK;
		}
		
		
		p1= new Point(Integer.parseInt(x1),Integer.parseInt(y1));
		p2= new Point(Integer.parseInt(x2),Integer.parseInt(y2));
			
	
	}
    
    /**
     * draws this object to the canvas
     */
	public void drawMe(Graphics g) {	
		//System.out.println("draw me");
		//Color tempColor = g.getColor();
		g.setColor(myColor);
		g.drawRect(Math.min(p1.x,p2.x), Math.min(p1.y,p2.y), Math.abs(p1.x-p2.x), Math.abs(p1.y-p2.y));
		//g.setColor(tempColor);
	}
	
	/**
	 * checks if point is in the bounding box of the object
	 */
	public boolean isPointInObject(Point p){
		return p.x<=Math.max(p1.x,p2.x) && p.x>=Math.min(p1.x,p2.x) && p.y>=Math.min(p1.y,p2.y) && p.y<=Math.max(p1.y,p2.y);
	}
	
	/**
	 * return height of bounding box
	 */
	public int getHeight() {
		return Math.abs(p1.y-p2.y);
	}

	/**
	 * return top of bounding box
	 */
	public int getTop() {
		return Math.min(p1.y,p2.y);
	}

	/**
	 * returns width of bounding box
	 */
	public int getWidth() {
		return Math.abs(p1.x-p2.x);
	}
	
	/**
	 * return the left side of the bounding box
	 */
	public int getLeft(){
		return Math.min(p1.x,p2.x);
	}
	
	/**
	 * set color of the object to c
	 */
	public void setmycolor(Color c){
		myColor=c;
	}
	
	/**
	 * move the anchor point by (+x, +y)
	 */
	public void moveAnchor(int x, int y) {
		p1.x+=x;
		p1.y+=y;
		p2.x+=x;
		p2.y+=y;
	}
}
